package cn.fan.testfunction;

import cn.fan.testfunction.countdown.QueueTask;
import cn.fan.testfunction.countdown.SeeDoctorTask;

import java.util.concurrent.CountDownLatch;

/**
 * （减计数）
 * countDownLatch.countDown();  计数器减一  变为0   countDownLatch.await();的线程就会唤醒
 * countDownLatch.await()  等待所有子线程完成，在在执行
 * <p>
 * 注释掉的是实现最大并行的方式
 * 未注释等待所有子线程完成，在在执行
 */
public class CountDownLaunchRunner {

    static int sub = 0;
    static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        long now = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(2);

        new Thread(new SeeDoctorTask(countDownLatch)).start();
        new Thread(new QueueTask(countDownLatch)).start();


//        for(int i=0;i<10;i++){
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        countDownLatch.await();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    synchronized (object){
//                        for(int j=0;j<1000;j++){
//                            sub++;
//                        }
//                    }
//
//                }
//            });
//        }
//        Thread.sleep(3000);

//        countDownLatch.countDown();
        countDownLatch.await();
        System.out.println("over，回家 cost:" + (System.currentTimeMillis() - now));
    }

}
